package com.example.demo.contorller;

import com.alibaba.fastjson.JSON;
import com.example.demo.Bean.Result;
import com.example.demo.Bean.Student;
import com.example.demo.Bean.Teacher;
import com.example.demo.service.LoginService;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.EOFException;
import java.util.Map;


@RestController
public class LoginController {

    @Autowired
    LoginService loginService;

    @Autowired
    DefaultKaptcha defaultKaptcha;

    //学生登录入口
    @PostMapping("/loginOfStudent")
    @CrossOrigin
    public Result loginOfStudent(Student student){
        try {
            return loginService.loginOfStudent(student.getStudentUid(), student.getPassword());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    //教师登录入口
    @PostMapping("/loginOfTeacher")
    @CrossOrigin
    public Result loginOfTeacher(Teacher teacher){
        try {
            return loginService.loginOfTeacher(teacher.getTeacherUid(), teacher.getPassword());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500,e.toString());
        }
    }

    @PostMapping("/modifyPwOfStudent")
    @CrossOrigin
    public Result modifyPwOfStudent(@RequestBody Map<String, String> params){
        String json = null;
        json = JSON.toJSONString(params.get("studentUid"));
        String studentUid = JSON.parseObject(json, String.class);
        json = JSON.toJSONString(params.get("newPw"));
        String newPw = JSON.parseObject(json, String.class);
        json = JSON.toJSONString(params.get("original"));
        String original = JSON.parseObject(json, String.class);
        try{
            return loginService.modifyPwOfStudent(studentUid, newPw, original);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.getMessage());
        }
    }

    @PostMapping("/modifyPwOfTeacher")
    @CrossOrigin
    public Result modifyPwOfTeacher(@RequestBody Map<String, String> params){
        String json = null;
        json = JSON.toJSONString(params.get("teacherUid"));
        String teacherUid = JSON.parseObject(json, String.class);
        json = JSON.toJSONString(params.get("newPw"));
        String newPw = JSON.parseObject(json, String.class);
        json = JSON.toJSONString(params.get("original"));
        String original = JSON.parseObject(json, String.class);
        try{
           return loginService.modifyPwOfTeacher(teacherUid, newPw, original);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(500, e.toString());
        }
    }


    @RequestMapping("/out")
    @CrossOrigin
    public Result logout(){
        return loginService.logout();
    }



    @RequestMapping("/captcha")
    @CrossOrigin
    public void captcha(HttpServletResponse response, HttpServletRequest request){
        response.setDateHeader("Expires",0);
        response.setHeader("Cache-Control","no-store,no-cache,must-revalidate");
        response.addHeader("Cache-Control","post-check=0,pre-check=0");
        response.setHeader("Pragma","no-cache");
        response.setContentType("image/jpeg");

        String text = defaultKaptcha.createText();
        request.getSession().setAttribute("captcha",text);
        BufferedImage image = defaultKaptcha.createImage(text);
        ServletOutputStream outputStream = null;
        try{
            outputStream = response.getOutputStream();
            ImageIO.write(image,"jpg",outputStream);
            outputStream.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(null!=outputStream){
                try {
                    outputStream.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

}
